package com.sctlqq.wuye.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sctlqq.wuye.entity.Repair;
import com.sctlqq.wuye.entity.Result;
import com.sctlqq.wuye.entity.User;
import com.sctlqq.wuye.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sctlqq/api/wuye/repair")
public class RepairController {

    @Autowired
    private RepairService repairService;

    /**
     * 根据业主id，查询所有 报修单
     * @param id
     * @return
     */
    @PostMapping("/getrepairlist")
    public Result<Object> getRepairList(@RequestBody Long id){
        return new Result<>(HttpStatus.OK.value(),"success",repairService.list(new QueryWrapper<Repair>().eq("uid",id).orderByDesc("release_time")));
    }

    /**
     * 管理员==>> 业务管理  报修信息查看，根据提交报修时间排序
     * @return
     */
    @GetMapping("/getallrepairlist")
    public Result<Object> getAllRepairList(){
        return new Result<>(HttpStatus.OK.value(),"success",repairService.list(new QueryWrapper<Repair>().orderByDesc("release_time")));
    }

    /**
     * 查询所有 新任务 状态的报修单
     *    新任务、已接单、已维修、已评价
     * @return
     */
    @GetMapping("/getnewrepairlist")
    public Result<Object> getNewRepairList(){
        return new Result<>(HttpStatus.OK.value(),"success",repairService.list(new QueryWrapper<Repair>().eq("status","新任务").orderByDesc("release_time")));
    }

    /**
     * 查询所有 是该id 维修人员的 且 status 不为“新任务” 的报修单
     * @param workerId
     * @return
     */
    @PostMapping("/getrunningandfinishrepairlist")
    public Result<Object> getRunningAndFinishRepairList(@RequestBody Long workerId){
        return new Result<>(HttpStatus.OK.value(),"success",repairService.list(new QueryWrapper<Repair>().eq("worker_id",workerId)
                                                                .not(i -> i.eq("status","新任务")).orderByDesc("release_time")));
    }


    /**
     * 根据报修单id，查询单个 报修单
     * @param id 报修单id
     * @return
     */
    @PostMapping("/getrepairbyid")
    public Result<Object> getRepairById(@RequestBody Long id){
        return new Result<>(HttpStatus.OK.value(),"success",repairService.getById(id));
    }

    /**
     * 通过给定对象 新增单个报修单
     * @param repair
     * @return
     */
    @PostMapping("/addrepair")
    public Result<Object> addRepair(@RequestBody Repair repair){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",repairService.save(repair));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

    /**
     * 修改报修单  接单 、完成报修 和 评价
     * @param repair 报修单信息对象
     * @return
     */
    @PutMapping("/updaterepair")
    public Result<Object> updateRepair(@RequestBody Repair repair){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",repairService.updateById(repair));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

}
